package com.zx.map.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Component;

import com.zx.map.dao.CreditRepayInfoDao;
import com.zx.map.dao.model.CreditRepayInfo;
import com.zx.map.dao.model.CreditRepayInfoExample;
import com.zx.map.service.CreditRepayInfoService;
import com.zx.map.service.model.CreditRepayInfoModel;
import com.zx.mobile.domain.CreditRepayInfoQueryDomain;
import com.zx.platform.utils.PmpUtils;

/**
 * Created by IntelliJ IDEA. User: Administrator Date: 14-3-1 Time: 上午11:07 To
 * change this template use File | Settings | File Templates.
 */
@Component("creditRepayInfoService")
public class CreditRepayInfoServiceImpl implements CreditRepayInfoService {

	@Autowired
	private CreditRepayInfoDao dao;
                                            	/**
	 * 根据Model查找列表
	 */
	public List<CreditRepayInfoModel> getListByModel(CreditRepayInfoModel model) throws DataAccessException {
		CreditRepayInfoExample example = new CreditRepayInfoExample();
		CreditRepayInfoExample.Criteria criteria = example.createCriteria();
        if(StringUtils.isNotBlank(model.getMerchantId()))
        {
             criteria.andMerchantIdEqualTo(model.getMerchantId());
        }
        if(StringUtils.isNotBlank(model.getBankAccount()))
        {
             criteria.andBankAccountEqualTo(model.getBankAccount());
        }
        if(StringUtils.isNotBlank(model.getNotifyFlag()))
        {
             criteria.andNotifyFlagEqualTo(model.getNotifyFlag());
        }
        if(StringUtils.isNotBlank(model.getNotifyDate()))
        {
             criteria.andNotifyDateEqualTo(model.getNotifyDate());
        }
		List<CreditRepayInfo> list = dao.getListByExample(example);
		List<CreditRepayInfoModel> modelList = new ArrayList<CreditRepayInfoModel>();
		for (CreditRepayInfo o : list) {
			CreditRepayInfoModel temp = new CreditRepayInfoModel();
			BeanUtils.copyProperties(o, temp);
			modelList.add(temp);
		}
		return modelList;
	}
	
	/**
	 * 新增或修改
	 */
	public void addOrUpdate(CreditRepayInfoModel model) throws DataAccessException {
		List<CreditRepayInfoModel> list = this.getListByModel(model);
		if(null != list && list.size() ==1)
		{
			model.setPaycardId(list.get(0).getPaycardId());
			this.update(model);
		}else{
			this.add(model);
		}
	}

	/**
	 * 新增
	 */
	public String add(CreditRepayInfoModel model) throws DataAccessException {
		CreditRepayInfo temp = new CreditRepayInfo();
		BeanUtils.copyProperties(model, temp);
		String key = dao.add(temp);
		return key;
	}

	/**
	 * 修改
	 */
	public String update(CreditRepayInfoModel model) throws DataAccessException {
		CreditRepayInfo temp = new CreditRepayInfo();
		BeanUtils.copyProperties(model, temp);
		dao.update(temp);
		return model.getPaycardId().toString();
	}

	/**
	 * 删除
	 */
	public void delete(CreditRepayInfoModel model) throws DataAccessException {
		CreditRepayInfo temp = new CreditRepayInfo();
		BeanUtils.copyProperties(model, temp);
		dao.delete(temp);
	}
	/**
     * 分页查询
     *
     * @param pageSize
     * @param pageNum
     * @return
     * @throws DataAccessException
     */
    public Map getPageByDomain(int pageSize, int pageNum, CreditRepayInfoQueryDomain CreditRepayInfoQueryDomain)
            throws DataAccessException {
        Map<String, Object> map = new HashMap<String, Object>();
        CreditRepayInfoExample example = new CreditRepayInfoExample();
        CreditRepayInfoExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(CreditRepayInfoQueryDomain.getMerSysAccount())) {
            criteria.andMerSysAccountEqualTo(CreditRepayInfoQueryDomain.getMerSysAccount());
        }
        int pageBegin = (pageNum - 1) * pageSize;
        List<CreditRepayInfo> list = dao.getPageByExample(pageSize, pageBegin, example);
        ;
        List<CreditRepayInfoModel> modelList = new ArrayList<CreditRepayInfoModel>();
        for (CreditRepayInfo o : list) {
            CreditRepayInfoModel temp = new CreditRepayInfoModel();
            BeanUtils.copyProperties(o, temp);
            modelList.add(temp);
        }
        int count = dao.countListByExample(example);
        map.put("count", count);
        map.put("list", modelList);
        return map;
    }

    public List<CreditRepayInfoModel> getListByDomain(
            CreditRepayInfoQueryDomain domain) throws DataAccessException {
        CreditRepayInfoExample example = new CreditRepayInfoExample();
        CreditRepayInfoExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(domain.getMerSysAccount())) {
            criteria.andMerSysAccountEqualTo(domain.getMerSysAccount());
        }
        if (StringUtils.isNotBlank(domain.getBeginDate())) {
            criteria.andCreateDateBetween(PmpUtils.strToDate(
            		domain.getBeginDate(), "yyyy-MM-dd"), PmpUtils
                                .addDate(PmpUtils.strToDate(domain.getEndDate(),
                                        "yyyy-MM-dd"), 1));
        }
        List<CreditRepayInfo> list = dao.getListByExample(example);
        List<CreditRepayInfoModel> modelList = new ArrayList<CreditRepayInfoModel>();
        for (CreditRepayInfo o : list) {
            CreditRepayInfoModel temp = new CreditRepayInfoModel();
            BeanUtils.copyProperties(o, temp);
            modelList.add(temp);
        }
        return modelList;
    }

	@Override
	public CreditRepayInfoModel getModelByKey(Integer key)
			throws DataAccessException {
		CreditRepayInfo o = dao.selectByPrimaryKey(key);
		CreditRepayInfoModel model = new CreditRepayInfoModel();
        if (o != null) {
            BeanUtils.copyProperties(o, model);
        }

        return model;
    }
}
